<template>
  <div class="overall">
    <el-card class="box-card">
      <div class="title"></div>
      <el-form ref="form" :rules="rules" :model="user">
        <el-form-item prop="username">
          <el-input
            v-model="user.username"
            prefix-icon="el-icon-user"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="user.password"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            v-model="user.repassword"
            prefix-icon="el-icon-lock"
            placeholder="请再次输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-button type="primary" @click="reg">注册</el-button>
        <el-link :underline="false" @click="$router.push('/login')">已有账号，点击登录</el-link>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { regAPI } from '@/api/user'
export default {
  name: 'MyReg',
  data() {
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.user.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      user: {
        username: '',
        password: '',
        repassword: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,10}$/, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ],
        repassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' },
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    reg() {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        const { data: res } = await regAPI(this.user)
        if (res.code === 0) {
          this.$message.success(res.message)
          this.$refs.form.resetFields()
          this.$router.push('/login')
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.overall {
  position: relative;
  height: 100%;
  background: url('@/assets/images/login_bg.jpg') no-repeat;
  background-size: cover;
  .el-card {
    position: absolute;
    width: 400px;
    // height: 300px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .title {
      background: url('@/assets/images/login_title.png') top center no-repeat;
      height: 40px;
    }
    .el-button {
      width: 100%;
    }
    .el-link {
      margin-top: 20px;
    }
  }
}
</style>
